﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QConnectSDK;
using System.Web.Security;
using PlugNT.Common;
using PlugNT.Cms.Model;
using FJASPX.Service;
using FJASPX.Framwork.Helper;
using FJASPX.Framwork.Config;

namespace PlugNT.Cms.Web
{
    public partial class QQRegister : System.Web.UI.Page
    {
        public IUserService iUserService { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            Init();
        }
        public void Init()
        {
            if (Session["QzoneOauth"] == null)
            {
                Response.Redirect("/Register.aspx");
            }
            QOpenClient qzone = (QOpenClient)Session["QzoneOauth"];
            if (qzone == null)
            {
                Response.Redirect("/Register.aspx");
            }
            if (qzone.OAuthToken == null)
            {
                Response.Redirect("/Register.aspx");

            } FJASPX.Domain.User user = iUserService.GetUserByOpenId(qzone.OAuthToken.OpenId);
            if (user != null)
            {
                FormsAuthentication.SetAuthCookie(user.UserName, false);
                if (Roles.IsUserInRole(user.UserName, ConfigBase.GetConfig("RoleMember")))
                {
                    Response.Redirect("/Member/Index.aspx");
                }
                if (Roles.IsUserInRole(user.UserName, ConfigBase.GetConfig("RolePersonal")))
                {
                    Response.Redirect("/Personal/Index.aspx");
                }
            }

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            QOpenClient qzone = (QOpenClient)Session["QzoneOauth"];
            string userName = this.username.Value.Trim();
            string userPassword = this.password.Value.Trim();
            if (RadioButtonList1.SelectedValue == "0")
            {
                //有账号
                FJASPX.Domain.User user = iUserService.GetUserByName(userName);
                if (Membership.ValidateUser(userName, userPassword) && user != null)
                { 
                    user.OpenId = qzone.OAuthToken.OpenId;
                    iUserService.Update(user);

                    FormsAuthentication.SetAuthCookie(userName, false);
                    if (Roles.IsUserInRole(userName, ConfigBase.GetConfig("RoleMember")))
                    {
                        Response.Redirect("/Member/Index.aspx");
                    }
                    if (Roles.IsUserInRole(userName, ConfigBase.GetConfig("RolePersonal")))
                    {
                        Response.Redirect("/Personal/Index.aspx");
                    }
                }
                else
                {
                    JScript.Alert("用户名或密码错误！", this.Page);
                }
            }
            else
            {
                MembershipCreateStatus creatStatude;
                string creatMessage = string.Empty;
                Membership.CreateUser(userName, this.password.Value.Trim(), DESEncrypt.Encrypt(this.email.Value.Trim()), "default", "default", true, out creatStatude);
                switch (creatStatude)
                {
                    case MembershipCreateStatus.DuplicateUserName:
                        creatMessage = "用户已经存在，请重新选择用户名";
                        break;
                    case MembershipCreateStatus.DuplicateEmail:
                        creatMessage = "该邮件地址已经注册，请重新填写邮件地址";
                        break;
                    case MembershipCreateStatus.InvalidPassword:
                        creatMessage = "密码的格式设置不正确";
                        break;
                    case MembershipCreateStatus.InvalidEmail:
                        creatMessage = "电子邮件地址的格式设置不正确";
                        break;
                    case MembershipCreateStatus.InvalidAnswer:
                        creatMessage = "密码提示问题答案的格式设置不正确";
                        break;
                    case MembershipCreateStatus.InvalidQuestion:
                        creatMessage = "密码提示问题的格式设置不正确";
                        break;
                    case MembershipCreateStatus.InvalidUserName:
                        creatMessage = "在数据库中未找到用户名";
                        break;
                    case MembershipCreateStatus.ProviderError:
                        creatMessage = "服务器出错，请稍候重试";
                        break;
                    case MembershipCreateStatus.UserRejected:
                        creatMessage = "因为提供程序定义的某个原因而未创建用户";
                        break;
                    case MembershipCreateStatus.Success:
                        Roles.AddUsersToRole(new string[] { userName }, ConfigBase.GetConfig("RoleMember"));


                        FJASPX.Domain.User user = new FJASPX.Domain.User();
                        user.UserName = userName;
                        user.PassWord = userPassword;
                        user.OpenId = qzone.OAuthToken.OpenId;
                        user.LoginTime = DateTime.Now;
                        user.State = 0;
                        user.Sex = qzone.GetCurrentUser().Gender;
                        user.Pic = qzone.GetCurrentUser().Figureurl;
                        iUserService.Save(user);

                        FormsAuthentication.SetAuthCookie(userName, false);
                        JScript.AlertAndRedirect("绑定成功！", "/Personal/Index.aspx", true);
                        break;
                    default:
                        creatMessage = "服务器端有错误发生，请稍候重试";
                        break;
                }
            }
        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (RadioButtonList1.SelectedValue == "0")
            {
                this.Span2.Visible = false;
            }
            else
            {
                this.Span2.Visible = true;
            }
        }
    }
}